package com.app.aipass.app;

import android.content.Context;

import androidx.multidex.MultiDex;

import com.app.aipass.BuildConfig;
import com.app.aipass.R;
import com.app.aipass.entity.ConfigEntity;
import com.app.aipass.entity.UserEntity;
import com.app.aipass.entity.WalletEntity;
import com.app.aipass.utils.ScreenUtil;
import com.app.aipass.utils.ShareUtil;
import com.app.aipass.utils.StatusBarUtil;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseApplication;
import me.goldze.mvvmhabit.crash.CaocConfig;
import me.goldze.mvvmhabit.utils.KLog;

/**
 * Created by goldze on 2017/7/16.
 */

public class AppApplication extends BaseApplication {

    private UserEntity mUserEntity;
    private List<WalletEntity> walletEntities;
    private Map<String,String> walletCoins = new HashMap<>();

    public static String priceType = "CNY";//美元USD  人民币CNY
    public static String pricetag = "¥";//美元$  人民币￥ ¥

    public static String userJinYongMsg = "";//用户被禁用是提示

    public static Map<String, ConfigEntity.LevelInfo> ad_level_exchange;
    public static int ad_exchange_level = 0;

    private Gson gson = new Gson();
    @Override
    public void onCreate() {
        super.onCreate();
        ScreenUtil.init(this);
        //是否开启打印日志
        KLog.init(BuildConfig.DEBUG);
        //初始化全局异常崩溃
        initCrash();
        //内存泄漏检测
//        if (!LeakCanary.isInAnalyzerProcess(this)) {
//            LeakCanary.install(this);
//        }
    }

    @Override
    protected void attachBaseContext(Context base)
    {
         super.attachBaseContext(base);
         MultiDex.install(this);
    }

    private void initCrash() {
        CaocConfig.Builder.create()
                .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //背景模式,开启沉浸式
                .enabled(true) //是否启动全局异常捕获
                .showErrorDetails(true) //是否显示错误详细信息
                .showRestartButton(true) //是否显示重启按钮
                .trackActivities(true) //是否跟踪Activity
                .minTimeBetweenCrashesMs(2000) //崩溃的间隔时间(毫秒)
                .errorDrawable(R.mipmap.ic_launcher) //错误图标
//                .restartActivity(LoginActivity.class) //重新启动后的activity
//                .errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity
//                .eventListener(new YourCustomEventListener()) //崩溃后的错误监听
                .apply();
    }

    public void setUserEntity(UserEntity entity)
    {
        mUserEntity = entity;
        if(entity != null)
        {
            if(gson == null)
            {
                gson = new Gson();
            }
            String jsonBDID = gson.toJson(entity);
            ShareUtil.getInstance().save("UserEntity",jsonBDID);
        }
    }
    public UserEntity getUserEntity()
    {
        if(mUserEntity != null)
        {
            return mUserEntity;
        }
        if(gson == null)
        {
            gson = new Gson();
        }
        String strUserEntity = ShareUtil.getInstance().get("UserEntity");
        UserEntity bdPushID2 = gson.fromJson(strUserEntity, UserEntity.class);
        return bdPushID2;
    }

    public void setOtcWalletAll(List<WalletEntity> entities)
    {
        walletEntities = entities;
    }
    public List<WalletEntity> getOtcWallet()
    {
        return walletEntities;
    }

    public WalletEntity getWalletEntityType(String type)
    {
        if(walletEntities != null && walletEntities.size() > 0)
        {
            for(int i = 0; i < walletEntities.size(); i++)
            {
                WalletEntity mWalletEntity = walletEntities.get(i);
                if(mWalletEntity.coin_name.equals(type))
                {
                    return mWalletEntity;
                }
            }
        }
        return null;
    }
    public void addWalletCoins(String key, String value)
    {
        walletCoins.put(key,value);
    }
    public void clearWalletCoins()
    {
        walletCoins.clear();
    }
//    private Map<String,String> getWalletCoins()
//    {
//        return walletCoins;
//    }
    public int getWalletCoinsNun()
    {
        return walletCoins.size();
    }
    public String getWalletCoinsName(String id)
    {
        if(walletCoins.size() < 1)
        {
            if(id.equals("1"))
            {
                return "用户账户";
            }
            else if(id.equals("2"))
            {
                return "发布账户";
            }
            else if(id.equals("3"))
            {
                return "AD";
            }
            else if(id.equals("4"))
            {
                return "AI";
            }
            else if(id.equals("5"))
            {
                return "积分";
            }
            return "";
        }
        for (Map.Entry<String,String> entry : walletCoins.entrySet())
        {
//            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
            if(entry.getValue().equals(id))
            {
                return entry.getKey();
            }
        }
        return "";
    }

    public String getWalletCoinsId(String name)
    {
        if(walletCoins.size() > 0 && walletCoins.containsKey(name))
        {
            return walletCoins.get(name);
        }
        else if(walletCoins.size() < 1)
        {
            if(name.equals("用户账户"))
            {
                return "1";
            }
            else if(name.equals("发布账户"))
            {
                return "2";
            }
            else if(name.equalsIgnoreCase("AD"))
            {
                return "3";
            }
            else if(name.equalsIgnoreCase("CSC"))
            {
                return "4";
            }
            else if(name.equals("积分"))
            {
                return "5";
            }else if (name.equals("AI")){
                return "7";
            }else if (name.equals("AI分红")){
                return "10";
            }
            return "";
        }

        return "";
    }
}
